package com.pengshun.modules.api.user.service.impl;

import com.pengshun.modules.api.mapper.PsUserInfoMapper;
import com.pengshun.modules.api.transport.dto.CarNumberDto;
import com.pengshun.modules.api.user.para.CarPara;
import com.pengshun.modules.api.user.repository.CarDrivingRepository;
import com.pengshun.modules.api.user.repository.OwnerDrivingRepository;
import com.pengshun.modules.api.user.repository.PsContractUserRepository;
import com.pengshun.modules.api.user.service.UserDriverService;
import com.pengshun.modules.api.user.service.dto.CarDrivingDto;
import com.pengshun.modules.api.user.service.dto.CarOwnerDto;
import com.pengshun.modules.api.user.service.dto.OwnerDrivingDto;
import com.pengshun.modules.api.user.service.dto.UserDriverDto;
import com.pengshun.modules.api.user.service.mapstruct.CarDrivingTodo;
import com.pengshun.modules.api.user.service.mapstruct.OwnerDrivingTodo;
import com.pengshun.modules.common.BaseResult;
import com.pengshun.modules.common.CarDrivingStatus;
import com.pengshun.modules.domain.CarDriving;
import com.pengshun.modules.domain.OwnerDriving;
import com.pengshun.modules.domain.PsContractUser;
import lombok.RequiredArgsConstructor;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Service
@RequiredArgsConstructor
public class UserDriverServiceImpl implements UserDriverService {


    private final PsUserInfoMapper userInfoMapper;
    private final CarDrivingRepository carDrivingRepository;
    private final CarDrivingTodo carDrivingTodo;
    private final OwnerDrivingRepository ownerDrivingRepository;
    private final OwnerDrivingTodo ownerDrivingTodo;
    private final MessageSource messageSource;
    private final PsContractUserRepository contractUserRepository;

    @Override
    public BaseResult list(CarPara para) {
        int page = (para.getPage()) * para.getSize();
        List<UserDriverDto> list = userInfoMapper.selectUserDriverList(para.getUserId(), para.getStatus(), page < 0 ? 0 : page, para.getSize());
        for (UserDriverDto dto : list) {
            if (dto.getStatus().longValue() == CarDrivingStatus.TO_ASSOCIATED) {
                //带关联
                if (para.getUserId().equals(dto.getCarUserId()) && dto.getType() == 1) {
                    dto.setShowContent("待自己同意");
                    dto.setIsShow(1);
                }
                if (para.getUserId().equals(dto.getCarUserId()) && dto.getType() == 2) {
                    dto.setShowContent("等待对方同意");
                    dto.setIsShow(0);
                }
            }
            if (dto.getStatus().longValue() == CarDrivingStatus.HAS_ASSOCIATED) {
                //带关联
                dto.setIsShow(1);
            }
            if (dto.getStatus().longValue() == CarDrivingStatus.TO_SOLUTION) {
                //带关联
                if (para.getUserId().equals(dto.getCarUserId()) && dto.getType() == 1) {
                    dto.setShowContent("对方拒绝");
                    dto.setIsShow(1);
                }
                if (para.getUserId().equals(dto.getCarUserId()) && dto.getType() == 2) {
                    dto.setShowContent("自己拒绝");
                    dto.setIsShow(0);
                }
            }
            if (dto.getStatus().longValue() == CarDrivingStatus.HAVE_UNBUNDLING) {
                //带关联
//                if (para.getUserId().equals(dto.getCarUserId()) && dto.getType() == 1) {
//                    dto.setShowContent("对方解绑");
//                }
                if (para.getUserId().equals(dto.getCarUserId())) {
                    dto.setShowContent("自己解绑");
                } else {
                    dto.setShowContent("对方解绑");
                }
            }
        }
        return new BaseResult(0, list);
    }

    @Override
    public BaseResult inners(OwnerDrivingDto para) {

        para.setApplyTime(new Date());
        if (para.getType() == 2) {
            //车主发起
            Long carId = para.getCarUserId();
            Long bossId = para.getDrivingUserId();
            para.setDrivingUserId(carId);
            para.setCarUserId(bossId);
        }
        if (para.getOwnerDrivingId() == null) {
            OwnerDriving driving = ownerDrivingRepository.findByDrivingUserIdAndCarUserId(para.getDrivingUserId(), para.getCarUserId());
            if (driving != null) {
                if (driving.getStatus() < 2) {
                    return new BaseResult(1, messageSource.getMessage("UserDriverServiceImpl.inners.driving", null, Locale.CHINA));
                } else {
                    driving.setStatus(0);
                    driving.setType(para.getType());
                    ownerDrivingRepository.save(driving);
                    return new BaseResult(0, messageSource.getMessage("UserDriverServiceImpl.inners.success", null, Locale.CHINA));
                }
            }
        }

        para.setStatus(CarDrivingStatus.TO_ASSOCIATED);
        para.setFreeze(1);
        OwnerDriving driving = ownerDrivingRepository.save(ownerDrivingTodo.toEntity(para));
        PsContractUser contractUser =contractUserRepository.findByOwnerDrivingId(driving.getOwnerDrivingId());
        if(contractUser!=null){
            contractUserRepository.delete(contractUser);
        }
//        PsContractUser contract=new PsContractUser();
//        contract.setOwnerDrivingId(driving.getOwnerDrivingId());
//        contract.setAgreeUserId();
//        contract.setApplyDate();
//        contract.setAgreeUserId();
        //
        return new BaseResult(0, messageSource.getMessage("UserDriverServiceImpl.inners.success", null, Locale.CHINA));

    }

    @Override
    public BaseResult save(CarDrivingDto para) {
        CarDriving driving = carDrivingRepository.findByCarOwnerIdAndOwnerDrivingId(para.getCarOwnerId(), para.getOwnerDrivingId());
        if (driving != null) {
            return new BaseResult(1, messageSource.getMessage("UserDriverServiceImpl.save.status", null, Locale.CHINA));
        }
        //查询当前车辆绑定的司机数量  每辆车最多关联2名司机
        List<CarDriving> list = carDrivingRepository.findByCarOwnerId(para.getCarOwnerId());
        if (list.size() == 2 || list.size() > 2) {
            return new BaseResult(1, messageSource.getMessage("UserDriverServiceImpl.save.more", null, Locale.CHINA));
        }
        para.setStatus(1);
        carDrivingRepository.save(carDrivingTodo.toEntity(para));
        return new BaseResult(0, messageSource.getMessage("operation.success", null, Locale.CHINA));
    }

    @Override
    public BaseResult freeze(CarDrivingDto para) {
        CarDriving driving = carDrivingRepository.findById(para.getCarDrivingId()).orElseGet(CarDriving::new);
        driving.setStatus(para.getStatus());
        carDrivingRepository.save(driving);
        return new BaseResult(0);
    }

    @Override
    public BaseResult notBindingCarList(UserDriverDto para) {
        List<CarNumberDto> list = new ArrayList<>();
        //查询司机绑定的 车辆关联id
        String carOwnerIds = userInfoMapper.selectCarOwnerIds(para.getOwnerDrivingId());
        if (para.getStatus() == 1) {
            if (carOwnerIds != null) {
                list = userInfoMapper.selectNotBindingDrivngList(para.getCarUserId(), carOwnerIds, para.getStatus());
            }
        } else {
            list = userInfoMapper.selectNotBindingDrivngList(para.getCarUserId(), carOwnerIds, para.getStatus());
        }
        for (CarNumberDto dto : list) {
            dto.setStatus(para.getStatus());
        }

        return new BaseResult(0, list);
    }

    @Override
    public BaseResult unbundling(CarDrivingDto para) {
        OwnerDriving ownerDriving = ownerDrivingRepository.findById(para.getOwnerDrivingId()).orElseGet(OwnerDriving::new);
        ownerDriving.setStatus(para.getStatus());
        ownerDriving.setCheckTime(new Date());
        ownerDrivingRepository.save(ownerDriving);
        List<CarDriving> list = carDrivingRepository.findByOwnerDrivingId(para.getOwnerDrivingId());
        if (list.size() > 0) {
            //删除已经关联的车辆
            carDrivingRepository.deleteInBatch(list);
        }
//        carDrivingRepository.deleteByOwnerDrivingId(ownerDriving.getOwnerDrivingId());
        //0:待关联，1同意，2拒绝，3解绑)
        String message="操作成功";
        if(para.getStatus().equals(2)){
            message="拒绝成功";
        }
        if(para.getStatus().equals(3)){
            message=messageSource.getMessage("UserDriverServiceImpl.unbundling.success", null, Locale.CHINA);
        }
        return new BaseResult(0, message);
    }
}
